package com.zb777.shuzhilingxi.utils;

import java.util.Arrays;
import java.util.Collections;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LlamaConfig {
    public String simpleMultiModalConversationCall(String imageUrl, String question)
            throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("image", imageUrl),
                        Collections.singletonMap("text", question+"使用中文并且只需要回答我的问题即可要求简练准确不要换行空行要求格式紧密"))).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // 若没有配置环境变量，请用阿里云百炼API Key将下行替换为：.apiKey("sk-xxx")
                .apiKey(System.getenv("DASH_SCOPE_API_KEY"))
                // 此处以llama-4-maverick-17b-128e-instruct为例，可按需更换模型名称。
                .model("llama-4-maverick-17b-128e-instruct")
                .message(userMessage)
                .build();
        MultiModalConversationResult result = conv.call(param);
        String text = (String) result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text");
        System.out.println(text);
        return text;
    }

}